package com.atguigu.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.edu.realtime.app.BaseStreamApp;
import com.edu.realtime.bean.TradeProvinceOrderBean;
import com.edu.realtime.common.Constant;
import com.edu.realtime.function.DimAsyncFunction;
import com.edu.realtime.util.AtguiguUtil;
import com.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

public class DwsTradeProvinceOrderWindow extends BaseStreamApp {
    public static void main(String[] args) {
        new DwsTradeProvinceOrderWindow().init(4002,2,"DwsTradeProvinceOrderWindow", Constant.TOPIC_DWD_TRADE_ORDER_DETAIL);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //解析成pojo类型
        SingleOutputStreamOperator<TradeProvinceOrderBean> beanStream = parseToPojo(stream);
        //分组，开窗，聚合
        SingleOutputStreamOperator<TradeProvinceOrderBean> beanStreamWithoutDims = windowAndAgg(beanStream);
        //补充维度信息
        SingleOutputStreamOperator<TradeProvinceOrderBean> beanStreamWithDims = joinDims(beanStreamWithoutDims);
        //写出到clickhouse
        beanStreamWithDims.addSink(FlinkSinkUtil.getClickHourseSink("dws_trade_province_order_window",TradeProvinceOrderBean.class));
    }

    private SingleOutputStreamOperator<TradeProvinceOrderBean> joinDims(SingleOutputStreamOperator<TradeProvinceOrderBean> beanStreamWithoutDims) {
        return AsyncDataStream.unorderedWait(
                beanStreamWithoutDims,
                new DimAsyncFunction<TradeProvinceOrderBean>() {
                    @Override
                    public String getTable() {
                        return "dim_base_province";
                    }

                    @Override
                    public String getId(TradeProvinceOrderBean input) {
                        return input.getProvinceId();
                    }

                    @Override
                    public void addDim(TradeProvinceOrderBean input, JSONObject dim) {
                        input.setProvinceName(dim.getString("NAME"));
                    }
                },
                60,
                TimeUnit.SECONDS
        );
    }

    private SingleOutputStreamOperator<TradeProvinceOrderBean> windowAndAgg(SingleOutputStreamOperator<TradeProvinceOrderBean> beanStream) {
        return beanStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<TradeProvinceOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((bean,ts) -> bean.getTs())
                )
                .keyBy(TradeProvinceOrderBean::getProvinceId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TradeProvinceOrderBean>() {
                            @Override
                            public TradeProvinceOrderBean reduce(TradeProvinceOrderBean value1, TradeProvinceOrderBean value2) throws Exception {
                                value1.setOrderAmount(value1.getOrderAmount().add(value2.getOrderAmount()));
                                value1.getOrderIdSet().addAll(value2.getOrderIdSet());
                                value1.getUserIdSet().addAll(value2.getUserIdSet());
                                return value1;
                            }
                        },
                        new ProcessWindowFunction<TradeProvinceOrderBean, TradeProvinceOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String s, Context context, Iterable<TradeProvinceOrderBean> elements, Collector<TradeProvinceOrderBean> out) throws Exception {
                                TradeProvinceOrderBean bean = elements.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(context.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(context.window().getEnd()));
                                bean.setOrderCount((long) bean.getOrderIdSet().size());
                                bean.setOrderUserCount((long) bean.getUserIdSet().size());
                                bean.setTs(System.currentTimeMillis());
                                out.collect(bean);
                            }
                        });
    }

    private SingleOutputStreamOperator<TradeProvinceOrderBean> parseToPojo(DataStreamSource<String> stream) {
        return stream.map(new MapFunction<String, TradeProvinceOrderBean>() {
            @Override
            public TradeProvinceOrderBean map(String value) throws Exception {
                JSONObject obj = JSON.parseObject(value);
                return TradeProvinceOrderBean.builder()
                        .orderIdSet(new HashSet<>(Collections.singletonList(obj.getString("order_id"))))
                        .userIdSet(new HashSet<>(Collections.singletonList(obj.getString("user_id"))))
                        .provinceId(obj.getString("province_id"))
                        .orderAmount(obj.getBigDecimal("final_amount"))
                        .ts(obj.getLong("ts")*1000)
                        .build();
            }
        });
    }
}
